package com.bike.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Field;

/**
 * java压缩成zip
 * 创建人：GRSUN创建时间：2015年1月14日
 * @version
 */
public class FileZip {
	    
	    /**
	     * 压缩单个文件
	     * @param file
	     * @param out
	     * @param suffix 
	     * @throws IOException
	     */
	    public static void doCompress(InputStream is, ZipOutputStream out, String suffix) throws IOException{
	        byte[] buffer = new byte[1024];
	        out.putNextEntry(new ZipEntry( UuidUtil.get32UUID().concat(suffix) ));//文件名可随意
	        int len = 0 ;
	        // 读取文件的内容, 打包到zip文件
	        while ((len = is.read(buffer)) > 0) {
	            out.write(buffer, 0, len);
	            out.flush();
	        }
	        out.closeEntry();
	        is.close();
	    }
	    
	
	   public static void zipAndDownload(String zipName,HttpServletResponse response,List<InputStream> iss,String suffix){
	    	response.setContentType("APPLICATION/OCTET-STREAM");  
	    	response.setHeader("Content-Disposition","attachment; filename="+zipName);
	    	ZipOutputStream out =null;
	    	try {
	    		out = new ZipOutputStream(response.getOutputStream());
	    		for(InputStream is:iss){
	    			doCompress(is, out,suffix);
	    			response.flushBuffer();
	    		}
	    	} catch (Exception e) {
	    		e.printStackTrace();
	    	}finally{
	    		if(out!=null){
	    			try {
	    				out.close();
	    			} catch (IOException e) {
	    				e.printStackTrace();
	    			}
	    		}
	    	}
	    }
	
	/**
	 * @param inputFileName 你要压缩的文件夹(整个完整路径)
	 * @param zipFileName 压缩后的文件(整个完整路径)
	 */
	public static void zip(String inputFileName, String zipFileName) throws Exception {
		zip(zipFileName, new File(inputFileName));
	}

	private static void zip(String zipFileName, File inputFile) throws Exception {
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
				zipFileName));
		zip(out, inputFile, "");
		out.flush();
		out.close();
	}

	private static void zip(ZipOutputStream out, File f, String base) throws Exception {
		if (f.isDirectory()) {
			File[] fl = f.listFiles();
			out.putNextEntry(new ZipEntry(base + "/"));
			base = base.length() == 0 ? "" : base + "/";
			for (int i = 0; i < fl.length; i++) {
				zip(out, fl[i], base + fl[i].getName());
			}
		} else {
			out.putNextEntry(new ZipEntry(base));
			FileInputStream in = new FileInputStream(f);
			int b;
			//System.out.println(base);
			while ((b = in.read()) != -1) {
				out.write(b);
			}
			in.close();
		}
	}
	
	 public static void main(String [] temp){       
		 try {           
			 zip("E:\\ftl","E:\\test.zip");//你要压缩的文件夹      和  压缩后的文件 
			 }catch (Exception ex) {       
				 ex.printStackTrace();    
			 }   
		}
}



//=====================文件压缩=========================
/*//把文件压缩成zip
File zipFile = new File("E:/demo.zip");
//定义输入文件流
InputStream input = new FileInputStream(file);
//定义压缩输出流	
ZipOutputStream zipOut = null;
//实例化压缩输出流,并制定压缩文件的输出路径  就是E盘下,名字叫 demo.zip
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
//设置注释
zipOut.setComment("www.demo.com");
int temp = 0;
while((temp = input.read()) != -1) {
	zipOut.write(temp);	
}		
input.close();
zipOut.close();*/
//==============================================
